<?php
namespace app\index\controller;

use app\common\service\Redis;

class ChatLog extends Init
{
    protected $groupChatLogModel;
    protected $friendChatLogModel;

    public function initialize(){
        parent::initialize();
        $this->groupChatLogModel = model('common/GroupChatLog');
        $this->friendChatLogModel = model('common/FriendChatLog');
    }

    public function index(){
        $id = $this->request->get('id',0);
        $type = $this->request->get('type',0);
        $page = $this->request->get('page',0);
        $off = $this->request->get('off',0);
        $user_id = session('user_id');

        $chat_log = [];
        $pages = [];
        $pages['limit'] = 10;
        $redis = new Redis();
        $has_chat = false;
        if($type == 'friend'){
            $key = 'rec:friend:'.getRecKeyName($user_id,$id);
            $contents = $redis->lRange($key,0,-1);
            $db_count = $this->friendChatLogModel->countById($id,$user_id);
            $pages['count'] = $db_count;
            if(!empty($contents)){
                if($page == 0) {
                    foreach ($contents as $i => $content) {
                        $content = json_decode($content, true);
                        $chat_log[$i]['id'] = $content['mine']['id'];
                        $chat_log[$i]['username'] = $content['mine']['username'];
                        $chat_log[$i]['avatar'] = $content['mine']['avatar'];
                        $chat_log[$i]['content'] = $content['mine']['content'];
                        $chat_log[$i]['timestamp'] = $content['to']['timestamp'];
                    }
                    $pages['curr'] = ceil($pages['count']/$pages['limit']);
                    $has_chat = true;
                }else{
                    $offset = ($page-1)*$pages['limit'] - ($pages['limit']-$off);
                    $limit = $pages['limit'];
                    if($offset < 0){
                        $offset = 0;
                        $limit = $off;
                    }
                    $chat_log = $this->formatFriendChat($id,$user_id,$offset,$limit);
                    $has_chat = true;
                    $pages['curr'] = $page;
                    $pages['off'] = $offset%$pages['limit'];
                }
                $pages['count'] += $pages['limit'];
            }else{
                if($page == 0){
                    $offset = $db_count-$pages['limit'];
                    if($db_count > 0){
                        $offset = $offset < 0 ? 0 :$offset;
                        $chat_log = $this->formatFriendChat($id,$user_id,$offset,$pages['limit']);
                        $has_chat = true;
                        $pages['curr'] = ceil($pages['count']/$pages['limit']);
                        $pages['off'] = $offset%$pages['limit'];
                    }else{
                        $has_chat = false;
                    }
                }else{
                    $offset = ($page-1)*$pages['limit'] - ($pages['limit']-$off);
                    $limit = $pages['limit'];
                    if($offset < 0){
                        $offset = 0;
                        $limit = $off;
                    }
                    $chat_log = $this->formatFriendChat($id,$user_id,$offset,$limit);
                    $has_chat = true;
                    $pages['curr'] = $page;
                    $pages['off'] = $off;
                }
            }
        }else{
            $yesterday = strtotime(date('Y-m-d')) - 1;
            $key = 'rec:group:'.$id;
            $contents = $redis->zRangeByScore($key, $yesterday, time());
            $db_count = $this->groupChatLogModel->countById($id);
            $pages['count'] = $db_count;
            if(!empty($contents)){
                if($page == 0){
                    foreach ($contents as $i=>$content){
                        $content = json_decode($content,true);
                        $chat_log[$i]['id'] = $content['mine']['id'];
                        $chat_log[$i]['username'] = $content['mine']['username'];
                        $chat_log[$i]['avatar'] = $content['mine']['avatar'];
                        $chat_log[$i]['content'] = $content['mine']['content'];
                        $chat_log[$i]['timestamp'] = $content['to']['timestamp'];
                    }
                    $pages['curr'] = ceil($pages['count']/$pages['limit']);
                    $has_chat = true;
                }else{
                    $offset = ($page-1)*$pages['limit'] - ($pages['limit']-$off);
                    $limit = $pages['limit'];
                    if($offset < 0){
                        $offset = 0;
                        $limit = $off;
                    }
                    $chat_log = $this->formatGroupChat($id,$offset,$limit);
                    $has_chat = true;
                    $pages['curr'] = $page;
                    $pages['off'] = $offset%$pages['limit'];
                }
                $pages['count'] += $pages['limit'];
            }else{
                if($page == 0){
                    $offset = $db_count-$pages['limit'];
                    if($db_count > 0){
                        $offset = $offset < 0 ? 0 :$offset;
                        $chat_log = $this->formatGroupChat($id,$offset,$pages['limit']);
                        $has_chat = true;
                        $pages['curr'] = ceil($pages['count']/$pages['limit']);
                        $pages['off'] = $offset%$pages['limit'];
                    }else{
                        $has_chat = false;
                    }
                }else{
                    $offset = ($page-1)*$pages['limit'] - ($pages['limit']-$off);
                    $limit = $pages['limit'];
                    if($offset < 0){
                        $offset = 0;
                        $limit = $off;
                    }
                    $chat_log = $this->formatGroupChat($id,$offset,$limit);
                    $has_chat = true;
                    $pages['curr'] = $page;
                    $pages['off'] = $off;
                }
            }
        }

        if($has_chat == false){
            return json(['code'=>-1,'msg'=>'','data'=>[]]);
        }

        return json(['code'=>0,'msg'=>'','data'=>$chat_log,'pages'=>$pages]);

    }

    public function formatGroupChat($id,$offset,$limit){
        $contents = $this->groupChatLogModel->getContentAsc($id,$offset,$limit);
        $chat_log = [];
        foreach ($contents as $i=>$content){
            $content = json_decode($content,true);
            $chat_log[$i]['id'] = $content['id'];
            $chat_log[$i]['username'] = $content['username'];
            $chat_log[$i]['avatar'] = $content['avatar'];
            $chat_log[$i]['content'] = $content['send_content'];
            $chat_log[$i]['timestamp'] = $content['send_time'] * 1000;
        }
        return $chat_log;
    }

    public function formatFriendChat($id,$user_id,$offset,$limit){
        $contents = $this->friendChatLogModel->getContentAsc($id,$user_id,$offset,$limit);
        $chat_log = [];
        foreach ($contents as $i=>$content){
            $content = json_decode($content,true);
            $chat_log[$i]['id'] = $content['id'];
            $chat_log[$i]['username'] = $content['username'];
            $chat_log[$i]['avatar'] = $content['avatar'];
            $chat_log[$i]['content'] = $content['send_content'];
            $chat_log[$i]['timestamp'] = $content['send_time'] * 1000;
        }
        return $chat_log;
    }
}
